﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace Biblio2.LocalParsing
{
    #region LocalParseResultDataRowComparer
    public class LocalParseResultDataRowComparer : IComparer
    {
        #region attribut
        private LocalParseResultComparer m_localParseResultComparer;
        #endregion attribut

        #region Constructeur
        public LocalParseResultDataRowComparer()
        {
            m_localParseResultComparer = new LocalParseResultComparer();
        }
        #endregion Constructeur

        #region IComparer Members

        public int Compare(object x, object y)
        {
            DataGridViewRow xRow = x as DataGridViewRow;
            DataGridViewRow yRow = y as DataGridViewRow;

            if (xRow == null || yRow == null)
                throw new ArgumentException();

            LocalParseResult xResult = xRow.Tag as LocalParseResult;
            LocalParseResult yResult = yRow.Tag as LocalParseResult;

            if (xResult == null || yResult == null)
                throw new ArgumentException();

            if (xResult.bParsingSuccess != yResult.bParsingSuccess)
            {
                if (xResult.bParsingSuccess == LocalParseResult.ValidityEnum.Missing_Critical)
                    return -1;
                else if (yResult.bParsingSuccess == LocalParseResult.ValidityEnum.Missing_Critical)
                    return 1;
                else if (xResult.bParsingSuccess == LocalParseResult.ValidityEnum.Missing_Optionnal)
                    return -1;
                else
                    return 1;
            }
            else
            {
                return m_localParseResultComparer.Compare(xResult, yResult);
            }
        }

        #endregion
    }
    #endregion LocalParseResultDataRowComparer

    #region LocalParseResultComparer
    public class LocalParseResultComparer : IComparer
    {
        #region IComparer Members

        public int Compare(object x, object y)
        {
            LocalParseResult xResult = x as LocalParseResult;
            LocalParseResult yResult = y as LocalParseResult;

            if (xResult == null || yResult == null)
                throw new ArgumentException();

            String xValue = xResult.GetValue(LocalParseResult.csSerieName);
            String yValue = yResult.GetValue(LocalParseResult.csSerieName);

            if ((xValue != String.Empty) && (yValue != String.Empty))
            {
                int result = xValue.CompareTo(yValue);
                if (result == 0)
                {
                    xValue = xResult.GetValue(LocalParseResult.csSeasonIndex);
                    yValue = yResult.GetValue(LocalParseResult.csSeasonIndex);

                    if ((xValue != String.Empty) && (yValue != String.Empty))
                    {
                        result = xValue.CompareTo(yValue);
                        if (result == 0)
                        {
                            xValue = xResult.GetValue(LocalParseResult.csSeasonIndex);
                            yValue = yResult.GetValue(LocalParseResult.csSeasonIndex);

                            if ((xValue != String.Empty) && (yValue != String.Empty))
                            {
                                result = xValue.CompareTo(yValue);

                                if (result != 0)
                                    return result;
                            }
                        }
                        else
                        {
                            return result;
                        }
                    }
                }
                else
                {
                    return result;
                }
            }

            return xResult.PathPair.FileName.CompareTo(yResult.PathPair.FileName);
        }

        #endregion
    }
    #endregion LocalParseResultSataRowComparer

}